﻿using System;
using System.Collections.Generic;
using Caffeine.Model;
using Caffeine.Model.Wpf;

namespace Caffeine.ChangeRecognitionPOC.ViewModel
{
    public class FormViewModel : ViewModelBase
    {
        #region State

        private readonly ActionCommand _resetGraphCommand;
        private readonly ActionCommand _commitGraphCommand;
        private readonly ViewModelCollection<FormViewModel> _childViewModels;
        private readonly ViewModelProperty<String> _name;
        private readonly ViewModelProperty<FormValue> _value;
        private readonly ViewModelProperty<Int32> _alternatingIndex;

        #endregion

        #region Constructors

        public FormViewModel()
        {
            _name = RegisterProperty<String>("Name", null);
            _value = RegisterProperty<FormValue>("Value");
            _alternatingIndex = RegisterProperty<Int32>("AlternatingIndex", null);
            _childViewModels = RegisterViewModelCollectionProperty<FormViewModel>("ChildViewModels");

            _resetGraphCommand = new ActionCommand(delegate { this.ResetGraph(); });
            _commitGraphCommand = new ActionCommand(delegate { this.CommitGraph(); });

            Value.ValidationMethod = ValueValidationMethod;
        }

        #endregion

        #region Public

        public ViewModelProperty<Int32> AlternatingIndex
        {
            get
            {
                return _alternatingIndex;
            }
        }

        public ViewModelProperty<String> Name
        {
            get
            {
                return _name;
            }
        }

        public ViewModelProperty<FormValue> Value
        {
            get
            {
                return _value;
            }
        }

        public ActionCommand ResetGraphCommand
        {
            get
            {
                return _resetGraphCommand;
            }
        }

        public ActionCommand CommitGraphCommand
        {
            get
            {
                return _commitGraphCommand;
            }
        }

        public ViewModelCollection<FormViewModel> ChildViewModels
        {
            get
            {
                return _childViewModels;
            }
        }

        #endregion

        #region Private

        private Boolean ValueValidationMethod(FormValue value, ViewModelProperty<FormValue> property, List<ValidationError> errors)
        {
            return value == FormValue.Valid;
        }

        #endregion
    }
}
